﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using ThouShaltNotBrick.Logic.GeneralObjects;
using GameControls;
using Microsoft.Xna.Framework.Graphics;
using ThouShaltNotBrick.Logic.Helpers;

namespace ThouShaltNotBrick.Logic.GameStates
{
    internal sealed class LossGameState : GameStateBase
    {
        #region Constants
        #endregion

        #region Properties
        private PlayData mPlayData;

        private Label mScoreLabel;
        private Button mContinueButton;
        #endregion

        #region Events
        #endregion

        #region Constructors
        #endregion

        #region Overriden Methods
        protected override void InitializeImpl()
        {
            mClearColor = Color.Black;

            mIsMouseVisible = true;

            InitializeControls();
        }

        protected override void DestroyImpl()
        {
            DestroyControls();
        }

        protected override void StartImpl(GameStateChangeData pChangeData)
        {
            mPlayData = (PlayData)pChangeData.Data;

            mScoreLabel.Text = string.Format("Score: {0:000000}", mPlayData.Score);
        }

        protected override void StopImpl()
        {
        }

        protected override void ResumeImpl(GameStateChangeData pChangeData)
        {
        }

        protected override void PauseImpl()
        {
        }

        protected override void HandleInputImpl(GameTime pGameTime)
        {
        }

        protected override void UpdateImpl(GameTime pGameTime)
        {
        }

        protected override void DrawImpl(GameTime pGameTime)
        {
        }
        #endregion

        #region Methods
        private void InitializeControls()
        {
            DestroyControls();

            int screenWidth = mGameManager.GetClientWidth();
            int screenHeight = mGameManager.GetClientHeight();

            SpriteFont youHaveLostFont = mFontResources.Font20;
            int youHaveLostLabelHeight = (int)youHaveLostFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle youHaveLostLabelRect = new Rectangle(0, screenHeight / 2 - youHaveLostLabelHeight, screenWidth, youHaveLostLabelHeight);
            Label youHaveLostLabel = new Label(youHaveLostLabelRect);
            youHaveLostLabel.SetText("You have lost!", youHaveLostFont, Color.Red);
            youHaveLostLabel.HorizontalAlignment = HorizontalAlignment.Center;
            youHaveLostLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(youHaveLostLabel);

            SpriteFont scoreFont = mFontResources.Font20;
            int scoreLabelHeight = (int)scoreFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle scoreLabelRect = new Rectangle(0, screenHeight / 2, screenWidth, scoreLabelHeight);
            mScoreLabel = new Label(scoreLabelRect);
            mScoreLabel.SetText(string.Empty, scoreFont, Color.Red);
            mScoreLabel.HorizontalAlignment = HorizontalAlignment.Center;
            mScoreLabel.VerticalAlignment = VerticalAlignment.Center;
            mControls.Add(mScoreLabel);

            SpriteFont continueButtonFont = mFontResources.Font20;
            int continueButtonWidth = 200;
            int continueButtonHeight = (int)continueButtonFont.MeasureString(Constants.DEFAULT_MEASURE_STRING).Y;
            Rectangle continueButtonRect = new Rectangle(screenWidth / 2 - continueButtonWidth / 2, scoreLabelRect.Bottom, continueButtonWidth, continueButtonHeight);
            mContinueButton = new Button(continueButtonRect, mGraphicsResources.WhiteTexture);
            mContinueButton.Label.SetText("Continue", continueButtonFont, Color.Red);
            mContinueButton.Pressed += ContinueButtonPressedHandler;
            mControls.Add(mContinueButton);
        }

        private void DestroyControls()
        {
            if (mContinueButton != null)
            {
                mContinueButton.Pressed -= ContinueButtonPressedHandler;
            }

            mControls.Clear();
        }
        #endregion

        #region Event Handlers
        private void ContinueButtonPressedHandler(object sender, EventArgs e)
        {
            mPlayData.CurrentLevel.ResetPadAndBall();
            GameStateManager.PopState();
        }
        #endregion

        #region Inner Classes
        #endregion
    }
}